﻿//png24 nav
if (!!window.DD_belatedPNG) {
    DD_belatedPNG.fix(".nav-filter, .nav dt a:hover, .nav dt.nav-account a:hover, .nav dt.nav-activity a:hover, .nav dt.nav-help a:hover,.nav-account dt.nav-account a, .nav-account dt.nav-account a:hover, .nav-activity dt.nav-activity a, .nav-activity dt.nav-activity a:hover, .nav-help dt.nav-help a, .nav-help dt.nav-help a:hover");
};

//element cache
var J_search = $("#J_search_form");
var J_search_popup = $("#J_search_popup");
var J_search_popup_noresult = $("#J_search_popup_noresult");
var _search_card = [];

$(document).ready(function () {

    //导航栏
    var J_nav = $("#J_nav");
    if (J_nav.length > 0) {
        //导航栏选中
        if ($("#J_nav_index").length > 0) {
            J_nav.addClass($("#J_nav_index").val());
        };
        //导航栏背景
        J_nav.find("dd a").hover(function () {
            var t = $(this);
            var h = J_nav.find(".nav-filter");
            if (J_nav.attr("class").indexOf(t.parent().attr("class")) > 0) { return; }
            var left = t.offset().left + t.width() / 2 - h.width() / 2 + 10;
            var top = t.offset().top + t.height() / 2 - h.height() / 2 - 10;
            h.show().animate({ left: left, top: top }, 100);
        }, function () {
            J_nav.find(".nav-filter").hide();
        });
    };

    //输入框
    J_search.find("input").each(function () {
        var t = $(this);
        t.val(t.attr("placeholder")).addClass("gray");
        t.focus(function () {
            if (t.val() == t.attr("placeholder")) {
                t.val("");
            };
            t.removeClass("gray");
            //加载js
            if (_search_card.length < 1) {
                var loadjs = J_search.attr("data-load");
                $.getScript(loadjs);
            };
        });
        t.blur(function () {
            if (t.val() == "") {
                t.val(t.attr("placeholder")).addClass("gray"); return;
            };
            if (t.val() == t.attr("placeholder")) {
                t.addClass("gray");
            };
        });
    });
    //无结果弹层
    J_search_popup_noresult.find(".popup-close").click(function () {
        J_search_popup_noresult.hide();
    });
    //搜索
    J_search.submit(function () {
        var J_searchinput = $(this).find("input");
        var J_eq = J_searchinput.attr("data-index");
        var J_searchresult = J_search_popup.find(".search-popup-bd");
        var J_searchresult_a = J_search_popup.find("a");

        var eq = parseInt(J_eq);
        var len = J_searchresult_a.length;
        //如果选有游戏
        action = $(this).attr("action");
        //如果有搜索结果但是没选择游戏，则默认选中第一项
        if (len > 0 && action.length < 1) {
            //得到选中项
            eq = eq < 0 ? 0 : eq;
            var sel = J_searchresult_a.eq(eq).html();
            action = J_searchresult_a.eq(eq).attr("href");
            //赋值			
            J_searchinput.val(sel);
            $(this).attr("action", action);
        };
        //得到游戏
        if (action.length > 4) {
            //隐藏
            J_searchresult.empty();
            J_search_popup.hide();
            return true;
        };
        J_search_popup_noresult.show();
        J_search_popup_noresult.find(".light-iframe").css("height", J_search_popup_noresult.height());
        return false;
    });
    //提示
    J_search.find("input").keyup(function (e) {
        if (_search_card.length < 1) {
            var loadjs = J_search.attr("data-load");
            $.getScript(loadjs);
            return;
        };
        var t = $(this);
        var index = t.attr("data-index");
        var val = t.val();
        var J_searchresult = J_search_popup.find(".search-popup-bd");
        var J_searchresult_a = J_search_popup.find("a");
        //取得选择索引
        index = parseInt(index);
        //取得搜索结果长
        var len = J_searchresult_a.length - 1;
        //上下键		
        if (len > -1) {//如果有值的时候处理上下键	
            //向下
            if (e.keyCode == 40) {
                J_searchresult_a.eq(index).removeClass("hover");
                index += 1;
                if (index > len) { index = 0; }
                J_searchresult_a.eq(index).addClass("hover");
                $(this).attr("data-index", index);
                J_search.attr("action", J_searchresult_a.eq(index).attr("href"));
                return;
            };
            //向上
            if (e.keyCode == 38) {
                J_searchresult_a.eq(index).removeClass("hover");
                index -= 1;
                if (index < 0) { index = len; }
                J_searchresult_a.eq(index).addClass("hover");
                $(this).attr("data-index", index);
                J_search.attr("action", J_searchresult_a.eq(index).attr("href"));
                return;
            };
        };
        //记录输入的东西 //清除索引
        t.attr("data-search", val).attr("data-index", "-1");
        //清除表单
        J_search.attr("action", "");
        //输入框无值清除pop
        if (val.length == 0) {
            J_searchresult.empty();
            J_search_popup.hide();
            return;
        }
        //匹配值
        x = -999;
        var aName = new Array();
        for (i = 1; i < _search_card.length; i++) {
            eq1 = _search_card[i].list.toLowerCase().indexOf(val.toLowerCase());
            eq2 = _search_card[i].name.toLowerCase().indexOf(val.toLowerCase());
            if ((eq1 > -1) || (eq2 > -1)) {
                if (eq2 > -1) {
                    _search_card[i].eq = x + eq2;
                } else {
                    _search_card[i].eq = eq1;
                }
                aName.push(_search_card[i]);
            }
        }
        //无匹配值清除
        if (aName.length == 0) {
            J_searchresult.empty();
            J_search_popup.hide();
            return;
        }
        //有匹配值//清除无结果框//排序
        J_search_popup_noresult.hide();
        aName.sort(compare());
        J_search_popup.show();
        J_searchresult.empty();
        //将匹配的提示结果显示
        var li = "";
        var win = window.screen.height;
        var max = win > 768 ? 9 : 5;
        var action = J_search.attr("data-action");
        for (var i = 0; i < aName.length; i++) {
            if (i > max) { break };
            li += "<a href='" + action + aName[i].gameId + "' target='_blank'>" + aName[i].name + "</a>";
        };
        li += "<p>约有<span>" + aName.length + "</span>件相关商品，请输入更多内容以便精确搜索</p>";
        J_searchresult.append(li);
        J_search_popup.find(".light-iframe").css("height", J_search_popup.height());
        //记录点击
        J_searchresult.find("a").click(function () {
            J_search.find("input").val($(this).html());
            J_search.attr("action", $(this).attr("href"));
            J_searchresult.empty();
            J_search_popup.hide();
        });
    }).blur(function () {
        setTimeout("J_search_popup.hide();", 500);
    }).focus(function () {
        if (J_search_popup.find("a").length > 0) { J_search_popup.show(); };
        J_search_popup_noresult.hide();
    });  
});
//数组排序
function compare() {
    return function comp(a1, a2) {
        var c1 = a1.eq;
        var c2 = a2.eq;
        if (c1 < c2) { return -1 };
        if (c1 > c2) { return 1 };
        return 0;
    }
};